package cn.test.service;

import cn.test.mapper.ActivityMapper;
import cn.test.mapper.ActivityVoMapper;
import cn.test.mapper.TypeMapper;
import cn.test.pojo.Activity;
import cn.test.pojo.ActivityVo;
import cn.test.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author:ggj
 * @Date:2020/04/07/21:18
 * @Description:
 */

@Service
@Transactional
public class ActivityService {

    @Autowired
    ActivityVoMapper activityVoMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    ActivityMapper activityMapper;

    //查询所有活动详情
    public List<ActivityVo> queryAll() throws Exception{
        List<ActivityVo> activityVos = activityVoMapper.queryAll();
        Calendar cal = Calendar.getInstance();
        for (ActivityVo act : activityVos){
            cal.setTime(act.getStartTime());
            cal.add(Calendar.HOUR_OF_DAY,act.getTime());
            act.setEndTime(cal.getTime());

            //跟当前时间进行比较
            //获取开始时间和结束时间
            Date startDate = act.getStartTime();
            Date endDate = act.getEndTime();
            Date date = new Date();
           /* SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = simpleDateFormat.format(date);
            Date parse = simpleDateFormat.parse(str);*/
            if (startDate.before(date)){
                act.setStartFlag(true);
            }

            if (endDate.before(date)){
                act.setEndFlag(true);
            }

            /*System.out.println(act.isStartFlag());
            System.out.println(act.isEndFlag());*/

        }
        return activityVos;
    }


    //增加活动
    public void addActivity(ActivityVo activityVo){
        String typeName = activityVo.getType();
        Type type = new Type();
        type.setType(typeName);
        Type type1 = typeMapper.selectOne(type);
        Activity activity = new Activity();

        activity.setTypeId(type1.getId());
        activity.setStartTime(activityVo.getStartTime());
        activity.setTime(activityVo.getTime());
        activity.setPlace(activityVo.getPlace());
        activity.setHumanNum(activityVo.getHumanNum());
        activity.setRemark(activityVo.getRemark());
        System.out.println(activity);
        activityMapper.insertSelective(activity);
    }

    //根据id删除活动
    public int delById(Integer id){
        Activity activity = new Activity();
        activity.setId(id);
        System.out.println(activity);
        return activityMapper.delete(activity);
    }

    //根据id查询活动
    public ActivityVo findById(Integer id){
        return activityVoMapper.findById(id);
    }

    //修改活动
    public void updateActivity(ActivityVo activityVo){
        String typeName1 = activityVo.getType();
        Type type0 = new Type();
        type0.setType(typeName1);
        Type type2 = typeMapper.selectOne(type0);
        Activity activity = new Activity();

        activity.setId(activityVo.getId());
        //System.out.println(activityVo.getId());
        activity.setTypeId(type2.getId());
        activity.setStartTime(activityVo.getStartTime());
        activity.setTime(activityVo.getTime());
        activity.setPlace(activityVo.getPlace());
        activity.setHumanNum(activityVo.getHumanNum());
        activity.setRemark(activityVo.getRemark());
        //System.out.println(activity);
        activityMapper.updateByPrimaryKey(activity);
    }
}
